package com.qdaily.ui.ui.views;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ExpandableListView;

import com.qdaily.supportlibrary.util.QLog;

/**
 * Created by song on 15/5/27.
 */
public class QDCommentView extends ExpandableListView
{

    private OnRefreshAndLoadMoreListener onRefreshAndLoadMoreListener;
    private QDLoadMoreView footView;
    private boolean mHasMore = true;
    public QDCommentView(Context context) {
        this(context, null);
    }

    public QDCommentView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public QDCommentView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initView();
    }

    private void initView() {

        this.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                return true;
            }
        });
        this.setGroupIndicator(null);

        this.setOnScrollListener(new OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {

            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                if (mHasMore && firstVisibleItem + visibleItemCount >= totalItemCount)
                {
                    scrollBottom();
                }
            }
        });

    }

    private void scrollBottom(){
        QLog.w("QDRefreshCommentView", "onLoadMore");
        if (onRefreshAndLoadMoreListener != null) {
            if (footView != null)
                footView.start();
            onRefreshAndLoadMoreListener.onLoadMore();
        }
    }


    /**
     * 是否还有下一页数据， 在onUIRefreshComplete调用，切记。
     */
    public void hasMoreLoadData(boolean hasMore) {
        mHasMore = hasMore;
        if (!hasMore)
        {
            if (footView != null)
            {
                footView.stop();
                footView.setVisibility(GONE);
                this.removeFooterView(footView);
                footView = null;
            }
        }
        else
        {
            if (footView == null)
            {
                footView = new QDLoadMoreView(getContext());
                footView.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.WRAP_CONTENT));
                this.addFooterView(footView);
            }
            footView.pause();
        }
    }

    /**
     * 设置回调监听
     *
     * @param onRefreshAndLoadMoreListener
     */
    public void setOnRefreshAndLoadMoreListener(OnRefreshAndLoadMoreListener onRefreshAndLoadMoreListener) {
        this.onRefreshAndLoadMoreListener = onRefreshAndLoadMoreListener;
    }
}
